package myJs

import japgolly.scalajs.react.vdom.all.VdomAttr
import myJs.myPkg.{ColumnOptions, Layer}
import myJs.myPkg.jquery._
import org.scalajs.dom.raw.ProgressEvent
import scalatags.Text.all._

import scala.math.BigDecimal.RoundingMode
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}


/**
 * Created by yz on 2019/3/6
 */
@JSExportTopLevel("Utils")
object Utils {

//  val Dashboard = g.Dashboard.asInstanceOf[Dashboard]

  val layer = g.layer.asInstanceOf[Layer]

  val checkColumn = ColumnOptions.field("state").checkbox(true)

  val dataToggle = VdomAttr("data-toggle")
  val dataContent = VdomAttr("data-content")
  val dataContainer = VdomAttr("data-container")
  val dataPlacement = VdomAttr("data-placement")
  val dataPercent = VdomAttr("data-percent")
  val dataShowPreview = VdomAttr("data-show-preview")
  val dataShowUpload = VdomAttr("data-show-upload")
  val dataHtml = VdomAttr("data-html")
  val dataAnimation = VdomAttr("data-animation")
  val dataTrigger = VdomAttr("data-trigger")
  val dataField = VdomAttr("data-field")
  val dataSortable = VdomAttr("data-sortable")
  val ariaExpanded = VdomAttr("aria-expanded")
  val ariaLabelledby = VdomAttr("aria-labelledby")
  val filterDataField = attr("filter-data-field")
  val sortType = attr("sort-type")
  val dataDismiss = VdomAttr("data-dismiss")
  val dataValue = VdomAttr("data-value")
  val dataFolder = VdomAttr("data-folder")
  val dataFileName = VdomAttr("data-fileName")
  val dataName = VdomAttr("data-name")
  val dataId = VdomAttr("data-id")
  val dataRow = VdomAttr("data-row")
  val dataInputName = VdomAttr("data-inputName")
  val dataMissionKind = VdomAttr("data-missionKind")
  val align = VdomAttr("align")
  val dataOn = VdomAttr("data-on")
  val dataOff = VdomAttr("data-off")
  val vRef = VdomAttr("ref")
  val ariaLabel = VdomAttr("aria-label")

  def progressHandlingFunction = {
    (e: ProgressEvent) => {
      if (e.lengthComputable) {
        val percent = e.loaded / e.total * 100
        val newPercent = BigDecimal(percent).setScale(2, RoundingMode.HALF_UP)
        $("#progress").html(s"(${newPercent}%)")
        if (percent >= 100) {
          $("#info").text("正在运行")
          $("#progress").html("")
        }
      }
    }
  }

  @JSExport("setColumns")
  def setColumns(value: String) = {
    val element = $(s"input:checkbox[value='${value}']")
    if (element.is(":checked")) {
      g.$("#table").bootstrapTable("showColumn", value)
    } else {
      g.$("#table").bootstrapTable("hideColumn", value)
    }

  }


}
